<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="<%=basePath%>resources/js/jquery-2.1.4.js"></script>
<script type="text/javascript" src="<%=basePath%>resources/js/jquery-2.1.4.min.js"></script>
<title>测试文件上传到fastdfs</title>
</head>
<script type="text/javascript">
	$(function(){
		$("#image").bind("click",function(){
			$("#file").click();
			$("#file").change(function(){
				$("#image").attr("src","<%=basePath%>resources/images/test2.png");
				uploadFile();
			})
		});
		
		$("#submit").bind("click",uploadFile);
	})
	function uploadFile(){
	 	var filename = $("#file").val();
		if(filename==null||filename==''){
			alert("请先上传文件");
			return false;
		}
		$.ajax({
			type: "POST",
		    url: "<%=basePath %>file/upload",
		    enctype: "multipart/form-data",
		    data: {
		    	file : filename
		    },
			success: function (msg) {
				$("#image").attr("src",msg);
			    alert("Data Uploaded: "+msg);
		    }
		});	 	
	}	
</script>
<body>
	<form action="<%=basePath %>file/upload" method="post" enctype="multipart/form-data">
		<img alt="" src="<%=basePath%>resources/images/test1.png" onclick="func()" width="200" height="200" id="image" name="image">
		<input type="file" style="display: none" id="file" name="file" />
		<input type="submit" value="点击上传" id="submit" />
	</form>
</body>
</html>